/**
 * 
 */
package fr.esiag.projet17.controller;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.Enumeration;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import fr.esiag.projet17.operation.IOperation;

/**
 * @author teddy
 * 
 */
public class DefaultController extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Properties props;

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		Enumeration<?> params = config.getInitParameterNames();
		Object param = params.nextElement();
		if (param instanceof String) {
			props = new Properties();
			try {
				props.load(new FileInputStream(getServletContext().getRealPath(
						config.getInitParameter(param.toString()))));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doGet(req, resp);

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/xml");
		try {
			System.out.println("Entrée dans la servlet");
			InputStream input = req.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(input));
			StringBuffer sBuffer = new StringBuffer();
			String tmp;
			while ((tmp = br.readLine()) != null) {
				sBuffer.append(tmp);
				System.out.println(sBuffer);
			}
			StringReader reader = new StringReader(sBuffer.toString());
			XMLInputFactory xif = XMLInputFactory.newInstance();
			XMLStreamReader xsr = xif.createXMLStreamReader(reader);
			JAXBContext context = JAXBContext.newInstance(props.getProperty("jaxb.context"));
			Unmarshaller unmarshaller = context.createUnmarshaller();
			IOperation<?> operation = (IOperation<?>) unmarshaller.unmarshal(xsr);
			System.out.println("Operation unmarshallisé");
			FutureTask<?> task = new FutureTask(operation);
			ExecutorService service = Executors.newSingleThreadExecutor();
			service.submit(task);
			Object transferable = task.get();
			System.out.println("Retour marshallisé");
			OutputStream output = resp.getOutputStream();
			Marshaller marsaller = context.createMarshaller();
			XMLOutputFactory xof = XMLOutputFactory.newInstance();
			XMLStreamWriter xsw = xof.createXMLStreamWriter(output);
			marsaller.marshal(transferable, xsw);
			xsw.flush();
			xsw.close();
		} catch (XMLStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void doPut(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPut(req, resp);
	}


	public static Properties getProps() {
		return props;
	}

}
